﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DARE.Save.DataStructures
{
    public class CEffectData : ISaveData
    {
        public bool PerPixelLighteningEnabled;
        public bool UseSceneParameters;
        public bool IsAnimationActive;
        public bool IsDecalsActive;
        public Vector3 EyePosition;
        public bool IsMultiTexturingActive;

        public Vector2 ColorMapTiles1;
        public Vector2 ColorMapOffset1;
        public string ColorMap1;
        public Vector2 ColorMapTiles2;
        public Vector2 ColorMapOffset2;
        public string ColorMap2;
        public Vector2 ColorMapTiles3;
        public Vector2 ColorMapOffset3;
        public string ColorMap3;
        public Vector2 MultiTexturingMapTiles;
        public Vector2 MultiTexturingMapOffset;
        public string MultiTexturingMap;

        public Matrix DecalsProjectorMatrix;
        public string DecalsMap;

        public float Brighteness;

        public bool LighteningEnabled;

        public Color AmbientLightColor;
        public float AmbientLightIntensity;
        public Color EmissiveLightColor;
        public float EmissiveLightIntensity;
        public Vector3 DirectionalLight1Direction;
        public Color DirectionalLight1DiffuseColor;
        public float DirectionalLight1DiffuseIntensity;
        public Color DirectionalLight1SpecularColor;
        public float DirectionalLight1SpecularIntensity;
        public Vector3 DirectionalLight2Direction;
        public Color DirectionalLight2DiffuseColor;
        public float DirectionalLight2DiffuseIntensity;
        public Color DirectionalLight2SpecularColor;
        public float DirectionalLight2SpecularIntensity;
        public Vector3 DirectionalLight0Direction;
        public Color DirectionalLight0DiffuseColor;
        public float DirectionalLight0DiffuseIntensity;
        public Color DirectionalLight0SpecularColor;
        public float DirectionalLight0SpecularIntensity;

        public Matrix[] Bones;
        public Matrix World;
        public Matrix View;
        public Matrix Projection;

        public Vector2 ColorMapOffset;
        public Vector2 ColorMapTiles;
        public string ColorMap;
        public float Alpha;

        public void FillDataStructure(object effect)
        {
            CEffect e = effect as CEffect;
            PerPixelLighteningEnabled = e.PerPixelLighteningEnabled;
            UseSceneParameters = e.UseSceneParameters;
            IsAnimationActive = e.IsAnimationActive;
            IsDecalsActive = e.IsDecalsActive;
            IsMultiTexturingActive = e.IsMultiTexturingActive;
            EyePosition = e.EyePosition;

            ColorMapTiles1 = e.ColorMapTiles1;
            ColorMapOffset1 = e.ColorMapOffset1;
            ColorMap1 = CResourceMgr.GetResourceName(e.ColorMap1);
            ColorMapTiles2 = e.ColorMapTiles2;
            ColorMapOffset2 = e.ColorMapOffset2;
            ColorMap2 = CResourceMgr.GetResourceName(e.ColorMap2);
            ColorMapTiles3 = e.ColorMapTiles3;
            ColorMapOffset3 = e.ColorMapOffset3;
            ColorMap3 = CResourceMgr.GetResourceName(e.ColorMap3);
            MultiTexturingMapTiles = e.MultiTexturingMapTiles;
            MultiTexturingMapOffset = e.MultiTexturingMapOffset;
            MultiTexturingMap = CResourceMgr.GetResourceName(e.MultiTexturingMap);

            DecalsProjectorMatrix = e.DecalsProjectorMatrix;
            DecalsMap = CResourceMgr.GetResourceName(e.DecalsMap);

            Brighteness = e.Brighteness;

            LighteningEnabled = e.LighteningEnabled;

            AmbientLightColor = e.AmbientLightColor;
            AmbientLightIntensity = e.AmbientLightIntensity;
            EmissiveLightColor = e.EmissiveLightColor;
            EmissiveLightIntensity = e.EmissiveLightIntensity;
            DirectionalLight1Direction = e.DirectionalLight0Direction;
            DirectionalLight0DiffuseColor = e.DirectionalLight0DiffuseColor;
            DirectionalLight0DiffuseIntensity = e.DirectionalLight0DiffuseIntensity;
            DirectionalLight0SpecularColor = e.DirectionalLight0SpecularColor;
            DirectionalLight0SpecularIntensity = e.DirectionalLight0SpecularIntensity;
            DirectionalLight1Direction = e.DirectionalLight1Direction;
            DirectionalLight1DiffuseColor = e.DirectionalLight1DiffuseColor;
            DirectionalLight1DiffuseIntensity = e.DirectionalLight1DiffuseIntensity;
            DirectionalLight1SpecularColor = e.DirectionalLight1SpecularColor;
            DirectionalLight1SpecularIntensity = e.DirectionalLight1SpecularIntensity;
            DirectionalLight2Direction = e.DirectionalLight2Direction;
            DirectionalLight2DiffuseColor = e.DirectionalLight2DiffuseColor;
            DirectionalLight2DiffuseIntensity = e.DirectionalLight2DiffuseIntensity;
            DirectionalLight2SpecularColor = e.DirectionalLight2SpecularColor;
            DirectionalLight2SpecularIntensity = e.DirectionalLight2SpecularIntensity;

            Bones = e.Bones;
            World = e.World;
            View = e.View;
            Projection = e.Projection;

            ColorMapOffset = e.ColorMapOffset;
            ColorMapTiles = e.ColorMapTiles;
            ColorMap = CResourceMgr.GetResourceName(e.ColorMap);
            Alpha = e.Alpha;
        }

        public void FillFromDataStructure(out object effect, params object[] paramsData)
        {
            CEffect e = new CEffect();

            e.PerPixelLighteningEnabled = PerPixelLighteningEnabled;
            e.UseSceneParameters = UseSceneParameters;
            e.IsAnimationActive = IsAnimationActive;
            e.IsDecalsActive = IsDecalsActive;
            e.IsMultiTexturingActive = IsMultiTexturingActive;
            e.EyePosition = EyePosition;

            if (ColorMap1 != null)
            {
                e.ColorMapTiles1 = ColorMapTiles1;
                e.ColorMapOffset1 = ColorMapOffset1;
                e.ColorMap1 = CResourceMgr.Load<Texture2D>(ColorMap1);
            }
            if (ColorMap2 != null)
            {
                e.ColorMapTiles2 = ColorMapTiles2;
                e.ColorMapOffset2 = ColorMapOffset2;
                e.ColorMap2 = CResourceMgr.Load<Texture2D>(ColorMap2);
            }
            if (ColorMap3 != null)
            {
                e.ColorMapTiles3 = ColorMapTiles3;
                e.ColorMapOffset3 = ColorMapOffset3;
                e.ColorMap3 = CResourceMgr.Load<Texture2D>(ColorMap3);
            }
            if (MultiTexturingMap != null)
            {
                e.MultiTexturingMapTiles = MultiTexturingMapTiles;
                e.MultiTexturingMapOffset = MultiTexturingMapOffset;
                e.MultiTexturingMap = CResourceMgr.Load<Texture2D>(MultiTexturingMap);
            }
            if (DecalsMap != null)
            {
                e.DecalsProjectorMatrix = DecalsProjectorMatrix;
                e.DecalsMap = CResourceMgr.Load<Texture2D>(DecalsMap);
            }
            e.Brighteness = Brighteness;
            
            e.LighteningEnabled = LighteningEnabled;
            
            e.AmbientLightColor = AmbientLightColor;
            e.AmbientLightIntensity = AmbientLightIntensity;
            e.EmissiveLightColor = EmissiveLightColor;
            e.EmissiveLightIntensity = EmissiveLightIntensity;
            e.DirectionalLight1Direction = DirectionalLight0Direction;
            e.DirectionalLight0DiffuseColor = DirectionalLight0DiffuseColor;
            e.DirectionalLight0DiffuseIntensity = DirectionalLight0DiffuseIntensity;
            e.DirectionalLight0SpecularColor = DirectionalLight0SpecularColor;
            e.DirectionalLight0SpecularIntensity = DirectionalLight0SpecularIntensity;
            e.DirectionalLight1Direction = DirectionalLight1Direction;
            e.DirectionalLight1DiffuseColor = DirectionalLight1DiffuseColor;
            e.DirectionalLight1DiffuseIntensity = DirectionalLight1DiffuseIntensity;
            e.DirectionalLight1SpecularColor = DirectionalLight1SpecularColor;
            e.DirectionalLight1SpecularIntensity = DirectionalLight1SpecularIntensity;
            e.DirectionalLight2Direction = DirectionalLight2Direction;
            e.DirectionalLight2DiffuseColor = DirectionalLight2DiffuseColor;
            e.DirectionalLight2DiffuseIntensity = DirectionalLight2DiffuseIntensity;
            e.DirectionalLight2SpecularColor = DirectionalLight2SpecularColor;
            e.DirectionalLight2SpecularIntensity = DirectionalLight2SpecularIntensity;
            
            e.Bones = Bones;
            e.World = World;
            e.View = View;
            e.Projection = Projection;

            if (ColorMap != null)
            {
                e.ColorMapOffset = ColorMapOffset;
                e.ColorMapTiles = ColorMapTiles;
                e.ColorMap = CResourceMgr.Load<Texture2D>(ColorMap);
            }
            e.Alpha = Alpha;




            effect = e;
        }
    }
}
